package com.leetcode.partition3;

import java.util.ArrayList;
import java.util.List;

/**
 * @author `RKC`
 * @date 2021/9/18 10:28
 */
public class LC241为运算表达式设计优先级 {

    public static List<Integer> diffWaysToCompute(String expression) {
        return divide(expression);
    }

    public static void main(String[] args) {
        String expression = "2*3-4*5";
        System.out.println(diffWaysToCompute(expression));
    }

    private static List<Integer> divide(String expression) {
        List<Integer> answer = new ArrayList<>();
        char[] expressionArr = expression.toCharArray();
        for (int i = 0; i < expressionArr.length; i++) {
            char ch = expressionArr[i];
            if (ch == '+' || ch == '-' || ch == '*') {              //以运算符进行划分
                List<Integer> answer1 = divide(expression.substring(0, i));
                List<Integer> answer2 = divide(expression.substring(i + 1));
                for (Integer ans1 : answer1) {
                    for (Integer ans2 : answer2) {
                        if (ch == '+') answer.add(ans1 + ans2);
                        else if (ch == '-') answer.add(ans1 - ans2);
                        else if (ch == '*') answer.add(ans1 * ans2);
                    }
                }
            }
        }
        if (answer.isEmpty()) answer.add(Integer.valueOf(expression));
        return answer;
    }
}
